// page/index/index.js
var isCheckIn = false //是否入住
var isCheckOut = false //是否离开
var checkInIndexMonth = 0 //入住点击月 index
var checkInIndexDay = 0 //入住点击天 index
var con = 0 // 选择日期的点击次数
var startDate = '' //入住日期的回传
var endDate = '' //离开日期的回传
Page({

  /**
   * 页面的初始数据
   *
   */
  data: {
    arr: [],
    sysW: null,
    lastDay: null,
    firstDay: null,
    weekArr: ['日', '一', '二', '三', '四', '五', '六'],
    year: null,
    calendarNum: 6,
    isRoomList: false
  },

  onLoad: function(options) {
    this.getIntentData(options)
    this.dateData();
    var res = wx.getSystemInfoSync();
    this.setData({
      sysW: res.windowHeight / 12, //更具屏幕宽度变化自动设置宽度
      marLet: this.data.firstDay,
      arr: this.data.arr
    });

    this.dateSelected();
  },
  getIntentData(options) {
    this.setData({
      isRoomList: options.isRoomList
    })
    if (this.data.isRoomList == 'false') {
      startDate = options.startTime
      endDate = options.endTime
    } else {
      startDate = options.startTime
      endDate = options.endTime
    }
  },
  /**
   * 日历数据
   */
  dateData: function() {
    let dataAll = [] //总日历数据
    let dataAll2 = [] //总日历数据
    let dataMonth = [] //月日历数据

    let date = new Date //当前日期
    let year = date.getFullYear() //当前年
    let week = date.getDay(); //当天星期几
    let weeks = []
    let month = date.getMonth() + 1 //当前月份
    let day = date.getDate() //当天
    console.log("当天日期： " + day)
    console.log("当天周期： " + week)

    let monthCount = 6 //一共显示多少月份

    let dayscNow = 0 //计数器
    let monthList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] //月份列表
    let nextMonthList = [] //需要跨年的下一年月份列表

    let nowMonthList = [] //本年剩余年份 

    //需要展示的日历月份列表
    var key = 0;
    for (var i = 0; i < monthCount; i++) {
      var dd = month + i;
      console.log('i : ' + i)
      if (dd < 13) {
        nowMonthList.push(dd)
        key = i;
        console.log('key: ' + key)
      } else {
        console.log("monthList[i - key]:" + monthList[i - key - 1])
        console.log('i - key : ' + (i - key))
        nextMonthList.push(monthList[i - key - 1])
      }
    }

    console.log('now: ' + nowMonthList)
    console.log('next: ' + nextMonthList)


    let yearList = [year] //年份最大可能
    if (month + monthCount > 13) {
      yearList.push(year + 1)
    }

    let leapYear = function(Year) { //判断是否闰年 
      if (((Year % 4) == 0) && ((Year % 100) != 0) || ((Year % 400) == 0)) { //闰年
        return (true);
      } else { //平年
        return (false);
      }
    }


    for (let i = 0; i < yearList.length; i++) { //遍历年
      let mList
      if (yearList[i] == year) { //判断当前年份
        mList = nowMonthList
      } else { //下一年
        mList = nextMonthList
      }
      console.log("mList  :   ————  " + mList)

      for (let j = 0; j < mList.length; j++) { //循环月份
        dataMonth = []
        //12个月份的天数的数组
        let t_days = [31, 28 + leapYear(yearList[i]), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

        let t_days_thisYear = []
        if (yearList[i] == year) { //当前年份的每个月的天数集合
          for (let m = 0; m < nowMonthList.length; m++) {
            t_days_thisYear.push(t_days[mList[m] - 1])
          }
          t_days = t_days_thisYear
        } else { //下一个年份的每个月的天数集合
          t_days = [31, 28 + leapYear(yearList[i]), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        }
        var months = ''
        if (mList[j] < 10) {
          months = '0' + mList[j]
        } else {
          months = mList[j]
        }
        console.log("months  :   ————  " + months)

        for (let k = 0; k < t_days[j]; k++) { //循环每天
          let nowData
          let days = k + 1
          if (days < 10) {
            days = "0" + days
          }

          if (yearList[i] == year && mList[j] == month) { //判断当年当月 ，
            if (k + 1 > day) { // 当天以后
              nowData = {
                year: yearList[i],
                month: months,
                day: k + 1,
                date: yearList[i] + "" + months + days,
                selected: 0,
                re: yearList[i] + "-" + months + "-" + days,
                dd: k + 1
              }
            } else if (k + 1 == day) { //当天
              nowData = {
                year: yearList[i],
                month: months,
                day: '今天',
                date: yearList[i] + "" + months + days,
                selected: 1, //当天
                re: yearList[i] + "-" + months + "-" + days,
                dd: '今天'
              }
              this.setData({
                getDate: yearList[i] + "-" + months + "-" + days
              })

            } else { //过期时间
              nowData = {
                year: yearList[i],
                month: months,
                day: k + 1,
                date: yearList[i] + "" + months + days,
                selected: 3, //无效日期
                re: yearList[i] + "-" + months + "-" + days,
                dd: k + 1
              }
            }
            dataMonth.push(nowData)
            if (k == 0) {
              let date = new Date(yearList[i] + "-" + months + "-" + k + 1)
              let weekss = date.getDay() //获取每个月第一天是周几
              weeks.push(weekss)
            }
          } else { //其他情况
            nowData = { //组装自己需要的数据
              year: yearList[i],
              month: months,
              day: k + 1,
              date: yearList[i] + "" + months + days,
              selected: 0,
              re: yearList[i] + "-" + months + "-" + days,
              dd: k + 1
            }
            dataMonth.push(nowData)
            if (k == 0) {
              let date = new Date(yearList[i] + "-" + months + "-" + k + 1)
              let weekss = date.getDay() //获取每个月第一天是周几
              weeks.push(weekss)
            }
          }
          console.log('dd:' + nowData)

        }
        dataAll.push(dataMonth)

      }
    }

    for (let i = 0; i < dataAll.length; i++) {
      if (dataAll[i].length != 0) {
        dataAll2.push(dataAll[i]);
      }
    }
    this.setData({
      date: dataAll2,
      weeks: weeks
    })
  },
  /**
   * 入住日期回传
   */
  dateSelected: function() {
    var that = this
    var months = this.data.date
    var inn = false
    for (var i = 0; i < months.length; i++) {
      var days = months[i]
      for (var j = 0; j < days.length; j++) {
        var selected = 'date[' + i + '][' + j + '].selected'
        var day = 'date[' + i + '][' + j + '].day'
        var dd = days[j].day
        var ddnew = days[j].dd
        if (startDate == days[j].re && endDate == days[j].re && this.data.isRoomList == 'false') {
          that.setData({
            [day]: '入-离',
            [selected]: 4
          })
        } else {
          if (startDate == days[j].re) {
            inn = true;
            if (this.data.isRoomList == 'false') {
              that.setData({
                [day]: '入住'
              })
            } else {
              that.setData({
                [day]: ddnew
              })
            }
          }
          if (inn) {
            that.setData({
              [selected]: 4
            })
          }
          if (endDate == days[j].re) {
            inn = false;
            if (this.data.isRoomList == 'false') {
              that.setData({
                [day]: '离开'
              })
            } else {
              that.setData({
                [day]: ddnew
              })
            }
            break;
          }
        }

      }
    }
  },
  /**
   * 清除选中
   */
  clearSlected() {
    var months = this.data.date
    var today = this.data.getDate
    var invalid = this.data.invalidDate
    var ss = startDate.split('-');
    var ee = endDate.split('-')
    var tt = today.split('-')
    var todayIn = parseInt(tt[1])

    var startIndex = parseInt(ss[1]) - todayIn;
    var endIndex = parseInt(ee[1]) - todayIn

    console.log(startIndex)
    console.log(endIndex)
    var inn = false
    var sel = 0
    for (var i = startIndex; i <= endIndex; i++) {
      var days = months[i]
      for (var j = 0; j < days.length; j++) {
        var dd = days[j].day + ''
        var ddnew = days[j].dd
        console.log('ddNea : ' + ddnew)
        var rr = days[j].re
        var selected = 'date[' + i + '][' + j + '].selected'
        var re = 'date[' + i + '][' + j + '].re'
        var day = 'date[' + i + '][' + j + '].day'
        if (days[j].selected == 3) {
          sel = 3
        } else if (days[j].re == today) {
          sel = 1;
        } else {
          sel = 0
        }
        this.setData({
          [selected]: sel
        })
        if (this.data.isRoomList == 'false') {
          console.log("11111 :" + dd)
          if (dd == '入住' || dd == '离开' || dd == '入-离') {
            console.log("2222 :" + (dd))
            this.setData({
              [day]: rr.split('-')[2]
            })
          } else if (rr == today) {
            console.log("3333 :" + (dd))
            this.setData({
              [day]: '今天'
            })
          } else {
            console.log("4444 :" + (dd))
            this.setData({
              [day]: dd
            })
          }
        } else {
          console.log("5555 :" + ddnew)
          this.setData({
            [day]: ddnew
          })
        }
      }
    }
  },
  clearbtn: function(e) {
    this.clearSlected();
    var pages = getCurrentPages()
    var curPage = pages[pages.length - 1]
    var prePage = pages[pages.length - 2]
    prePage.setData({
      dateType: '入住日期',
      startTime: '',
      endTime: '',
      bingPage: 1,
      messageCode: "",
      list: null,
      isGetData: false,
      iss: true
    })
    prePage.connectNetForData()
    wx.navigateBack({
      delta: 1
    })
  },

  selectday: function(e) {
    console.log(this.data.date)
    if (con == 0) {
      this.clearSlected();
    }
    console.log(e)
    var indexMonth = e.currentTarget.dataset.index
    var indexDay = e.currentTarget.dataset.indexs
    var isSelcted = this.data.date[indexMonth][indexDay].selected
    console.log(this.data.date[indexMonth][indexDay])
    if (isSelcted != 3) {
      con = con + 1
      var select = 4
      var selected = 'date[' + indexMonth + '][' + indexDay + '].selected'
      var dayTip = 'date[' + indexMonth + '][' + indexDay + '].day'
      if (!isCheckIn && this.data.isRoomList == 'false') {
        isCheckIn = true
        isCheckOut = true
        checkInIndexMonth = indexMonth
        checkInIndexDay = indexDay
        this.setData({
          [selected]: select,
          [dayTip]: '入住'
        })
      }
      if (!isCheckIn && this.data.isRoomList == 'true') {
        isCheckIn = true
        isCheckOut = true
        checkInIndexMonth = indexMonth
        checkInIndexDay = indexDay
        this.setData({
          [selected]: select
        })
      }
      var istrue = false
      if (checkInIndexMonth < indexMonth) {
        istrue = true;
      } else if (checkInIndexMonth == indexMonth && checkInIndexDay <= indexDay) {
        istrue = true;
      } else {
        istrue = false;
      }

      if (istrue) {
        if (isCheckOut && this.data.isRoomList == 'false') {
          if (checkInIndexMonth == indexMonth && checkInIndexDay == indexDay && con > 1) {
            var selects = 4
            var sel = 'date[' + indexMonth + '][' + indexDay + '].selected'
            var dayT = 'date[' + indexMonth + '][' + indexDay + '].day'
            this.setData({
              [sel]: selects,
              [dayTip]: '入-离'
            })
            isCheckIn = false
            con = 0
            var pages = getCurrentPages()
            var curPage = pages[pages.length - 1]
            var prePage = pages[pages.length - 2]
            var checkInDate = this.data.date[checkInIndexMonth][checkInIndexDay].re
            prePage.setData({
              checkInOutDate: checkInDate + '至' + checkInDate,
              colorCheck: 'rgb(51,51,51)',
              startTime: checkInDate,
              endTime: checkInDate
            })

            wx.navigateBack({
              delta: 1
            })
          } else if (con > 1) {
            console.log('checkInIndexMonth : ' + checkInIndexMonth)
            console.log('checkInIndexDay')
            for (var i = checkInIndexMonth; i <= indexMonth; i++) { //月份的遍历
              var jj
              if (i == checkInIndexMonth) {
                jj = checkInIndexDay
              } else {
                jj = 0
              }
              for (var j = jj; j < this.data.date[i].length; j++) { //日的遍历
                var selects = 4
                var sel = 'date[' + i + '][' + j + '].selected'
                var dayT = 'date[' + i + '][' + j + '].day'

                if (i == indexMonth && j == indexDay) {
                  this.setData({
                    [sel]: selects,
                    [dayTip]: '离开'
                  })
                  isCheckIn = false
                  con = 0
                  var pages = getCurrentPages()
                  var curPage = pages[pages.length - 1]
                  var prePage = pages[pages.length - 2]
                  var checkInDate = this.data.date[checkInIndexMonth][checkInIndexDay].re
                  var checkOutDate = this.data.date[i][j].re
                  prePage.setData({
                    checkInOutDate: checkInDate + '至' + checkOutDate,
                    colorCheck: 'rgb(51,51,51)',
                    startTime: checkInDate,
                    endTime: checkOutDate
                  })

                  wx.navigateBack({
                    delta: 1
                  })
                } else {
                  this.setData({
                    [sel]: selects
                  })
                }
              }
            }
          }
        }

        if (isCheckOut && this.data.isRoomList == 'true') {
          if (checkInIndexMonth == indexMonth && checkInIndexDay == indexDay && con > 1) {
            var selects = 4
            var sel = 'date[' + indexMonth + '][' + indexDay + '].selected'
            this.setData({
              [sel]: selects
            })
            isCheckIn = false
            con = 0
            var pages = getCurrentPages()
            var curPage = pages[pages.length - 1]
            var prePage = pages[pages.length - 2]
            var checkInDate = this.data.date[checkInIndexMonth][checkInIndexDay].re
            var mm = checkInDate.split('-')
            console.log(mm)
            prePage.setData({
              dateType: mm[1] + '.' + mm[2] + '-' + mm[1] + '.' + mm[2],
              startTime: checkInDate,
              endTime: checkInDate,
              bingPage: 1,
              list: null,
              messageCode: "",
              isGetData: false,
              iss: true
            })
            prePage.connectNetForData()
            wx.navigateBack({
              delta: 1
            })
          } else if (con > 1) {
            for (var i = checkInIndexMonth; i <= indexMonth; i++) { //月份的遍历
              var jj
              if (i == checkInIndexMonth) {
                jj = checkInIndexDay
              } else {
                jj = 0
              }
              for (var j = jj; j < this.data.date[i].length; j++) { //日的遍历
                var selects = 4
                var sel = 'date[' + i + '][' + j + '].selected'
                if (i == indexMonth && j == indexDay) {
                  this.setData({
                    [sel]: selects,
                  })
                  isCheckIn = false
                  con = 0
                  var pages = getCurrentPages()
                  var curPage = pages[pages.length - 1]
                  var prePage = pages[pages.length - 2]
                  var checkInDate = this.data.date[checkInIndexMonth][checkInIndexDay].re
                  var checkOutDate = this.data.date[i][j].re
                  var mm = checkInDate.split('-')
                  var dd = checkOutDate.split('-')
                  prePage.setData({
                    dateType: mm[1] + '.' + mm[2] + '-' + dd[1] + '.' + dd[2],
                    startTime: checkInDate,
                    endTime: checkOutDate,
                    bingPage: 1,
                    messageCode: "",
                    list: null,
                    isGetData: false,
                    iss: true
                  })
                  prePage.connectNetForData()
                  wx.navigateBack({
                    delta: 1
                  })
                } else {
                  this.setData({
                    [sel]: selects
                  })
                }
              }
            }
          }
        }
      }


    }
  }
})